﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Command;

namespace Songhay.Silverlight.BiggestBox.AdditionalSamplesPart.ViewModels
{
    using Songhay.Mvvm.ViewModels;

    /// <summary>
    /// Slideshow sample View Model
    /// </summary>
    public class SlideshowSampleViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SlideshowSampleViewModel"/> class.
        /// </summary>
        /// <remarks>
        /// For details, see
        /// “How to: Implement Property Change Notification”
        /// http://msdn.microsoft.com/en-us/library/ms743695.aspx
        /// A sample <c>set</c> block:
        /// <code>
        /// set
        /// {
        /// _myProperty = value;
        /// OnPropertyChanged("MyProperty");
        /// }
        /// </code>
        /// </remarks>
        public SlideshowSampleViewModel()
        {
            this.SlidesCollection = new ObservableCollection<string>
            {
                "/Songhay.Silverlight.BiggestBox.AdditionalSamplesPart;component/Bitmaps/FreshwaterInTheMountains.jpg",
                "/Songhay.Silverlight.BiggestBox.AdditionalSamplesPart;component/Bitmaps/HengshanMountainNanyue.jpg",
                "/Songhay.Silverlight.BiggestBox.AdditionalSamplesPart;component/Bitmaps/MountainsByTheWater.png"
            };
            this.SlidesSelectedIndex = 0;

            #region commanding:

            this.SlidesToggleCommand = new RelayCommand(
            () =>
            {
                if(this._slidesTimer == null) return;

                if(this._slidesTimer.IsEnabled) this._slidesTimer.Stop();
                else if(!this._slidesTimer.IsEnabled) this._slidesTimer.Start();

                this.IsDoingSlidesTick = this._slidesTimer.IsEnabled;
            });

            this.ViewLoadedCommand = new RelayCommand(
            ()=>
            {
                if(!this._slidesTimer.IsEnabled) this._slidesTimer.Start();
                this.IsDoingSlidesTick = this._slidesTimer.IsEnabled;
            });

            this.ViewUnloadedCommand = new RelayCommand(
            ()=>
            {
                if(this._slidesTimer.IsEnabled) this._slidesTimer.Stop();
                this.IsDoingSlidesTick = this._slidesTimer.IsEnabled;
            });

            #endregion

            #region timing:

            this._slidesTimer = new DispatcherTimer();

            this._slidesTimer.Interval = TimeSpan.FromSeconds(3);
            this._slidesTimer.Tick += (s, args) =>
            {
                var newIndex = ++this._slidesSelectedIndex;
                if(newIndex >= this._slidesCollection.Count) newIndex = 0;
                this.SlidesSelectedIndex = newIndex;
            };

            this.IsDoingSlidesTick = this._slidesTimer.IsEnabled;

            #endregion
        }

        /// <summary>
        /// Gets or sets the slides collection.
        /// </summary>
        /// <value>The slides collection.</value>
        public ObservableCollection<string> SlidesCollection
        {
            get { return this._slidesCollection; }
            set
            {
                this._slidesCollection = value;
                base.RaisePropertyChanged("SlidesCollection");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is doing slides tick.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is doing slides tick; otherwise, <c>false</c>.
        /// </value>
        public bool IsDoingSlidesTick
        {
            get { return this._isDoingSlidesTick; }
            set
            {
                this._isDoingSlidesTick = value;
                base.RaisePropertyChanged("IsDoingSlidesTick");
            }
        }

        /// <summary>
        /// Gets or sets the index of the slides selected.
        /// </summary>
        /// <value>The index of the slides selected.</value>
        public int SlidesSelectedIndex
        {
            get { return this._slidesSelectedIndex; }
            set
            {
                this._slidesSelectedIndex = value;
                base.RaisePropertyChanged("SlidesSelectedIndex");
            }
        }

        /// <summary>
        /// Gets or sets the slides toggle command.
        /// </summary>
        /// <value>The slides toggle command.</value>
        public ICommand SlidesToggleCommand { get; private set; }

        /// <summary>
        /// Gets or sets the view focus command.
        /// </summary>
        /// <value>The view focus command.</value>
        public ICommand ViewLoadedCommand { get; private set; }

        /// <summary>
        /// Gets or sets the view lost focus command.
        /// </summary>
        /// <value>The view lost focus command.</value>
        public ICommand ViewUnloadedCommand { get; private set; }

        ObservableCollection<string> _slidesCollection;
        int _slidesSelectedIndex;
        DispatcherTimer _slidesTimer;
        bool _isDoingSlidesTick;
    }
}
